package com.pswv2.socket;

import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;

public class SocketThread extends Thread{
	public static ServerSocket serverSocket = null;  
    public SocketThread(ServerSocket serverScoket){  
        try {  
            if(null == serverSocket){  
                serverSocket = new ServerSocket(8834);  
                System.out.println("socket start");
            }  
        } catch (Exception e) {  
            System.out.println("SocketThread创建socket服务出错");  
            e.printStackTrace();  
        }  
  
    }  
    
    public void run(){
    	System.out.println("socket start1");
		while(!this.isInterrupted()){
			System.out.println("socket start2");
			try {
				Socket socket = serverSocket.accept();
				
				if(null != socket && !socket.isClosed()){   
					//处理接受的数据
					new SocketOperate(socket).start();
				}
				socket.setSoTimeout(30000);
				
			}catch (Exception e) {
				e.printStackTrace();
			}
		}
	}

      
      
    public void closeSocketServer(){  
       try {  
            if(null!=serverSocket && !serverSocket.isClosed())  
            {  
             serverSocket.close();  
            }  
       } catch (IOException e) {  
        // TODO Auto-generated catch block  
        e.printStackTrace();  
       }  
     }  
}
